package org.huntaway.lucene;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.huntaway.lucene.document.impl.FileDocument;
import org.huntaway.lucene.document.impl.JavaDocument;
import org.huntaway.lucene.document.impl.TextDocument;

public class IndexFiles {
	private IndexFiles() {
	}

	static final String INDEX_DIR = "I:/jskyme/org.apache.lucene/huntaway/";
	static List<String> docsLst = new ArrayList<String>();
	static {
		docsLst.add("f:/backup/");
		docsLst.add("f:/Intel/");
		docsLst.add("f:/jskyme/");
		docsLst.add("f:/workspace/");
		docsLst.add("f:/YL/");
		docsLst.add("G:/");
		docsLst.add("H:/");
		docsLst.add("I:/backup");
		docsLst.add("I:/iso");
		docsLst.add("I:/tools");
		docsLst.add("L:/");
	}
	IndexWriter rawWriter = null;
	IndexWriter writer = null;
	IndexWriter textWriter = null;
	IndexWriter javaWriter = null;

	private void doIndex() {
		for (int i = 0; i < docsLst.size(); i++) {
			String docItem = docsLst.get(i);
			File docDir = new File(docItem);
			Date start = new Date();
			try {
				IndexWriterConfig rawInwc = newConfig();
				RAMDirectory ramDir = new RAMDirectory();
				rawWriter = buildWriter(ramDir, rawInwc);

				IndexWriterConfig inwc = newConfig();
				IndexWriterConfig inwctext = newConfig();
				IndexWriterConfig inwcjava = newConfig();
				writer = buildWriter(INDEX_DIR + "defualt", inwc);
				textWriter = buildWriter(INDEX_DIR + "text", inwctext);
				javaWriter = buildWriter(INDEX_DIR + "java", inwcjava);
				System.out.println("Indexing to directory '" + INDEX_DIR
						+ "'...");
				indexDocs(docDir);
				System.out.println("Optimizing...");
				writer.optimize();
				writer.close();
				textWriter.optimize();
				textWriter.close();
				javaWriter.optimize();
				javaWriter.close();

				Date end = new Date();
				System.out.println(end.getTime() - start.getTime()
						+ " total milliseconds");

			} catch (IOException e) {
				System.out.println(" caught a " + e.getClass()
						+ "\n with message: " + e.getMessage());
			}
		}
	}

	private IndexWriter buildWriter(String indexDir,
			IndexWriterConfig indexWriterConfig) throws CorruptIndexException,
			LockObtainFailedException, IOException {
		return buildWriter(FSDirectory.open(new File(indexDir)),
				indexWriterConfig);
	}

	private IndexWriter buildWriter(Directory dir,
			IndexWriterConfig indexWriterConfig) throws CorruptIndexException,
			LockObtainFailedException, IOException {
		return new IndexWriter(dir, indexWriterConfig);
	}

	private IndexWriterConfig newConfig() {
		return new IndexWriterConfig(Version.LUCENE_33, new StandardAnalyzer(
				Version.LUCENE_33));
	}

	public static void main(String[] args) {
		IndexFiles indexFile = new IndexFiles();
		indexFile.doIndex();
	}

	void indexDocs(File file) throws IOException {
		if (file.canRead()) {
			if (file.isDirectory()) {
				String[] files = file.list();
				// an IO error could occur
				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						indexDocs(new File(file, files[i]));
					}
				}
			} else {
				System.out.println("adding " + file);
				try {
					addDocument(file);
				} catch (FileNotFoundException fnfe) {
					;
				}
			}
		}
	}

	private void addDocument(File file) throws CorruptIndexException,
			IOException {
		rawWriter.addDocument(new FileDocument().document(file));
		if (file.getName().endsWith(".text")) {
			textWriter.addDocument(new TextDocument().document(file));
		}
		if (file.getName().endsWith(".java")) {
			javaWriter.addDocument(new JavaDocument().document(file));
		}
		writer.addDocument(new FileDocument().document(file));
	}

}
